package com.gh.manager.train.service.impl;

import java.util.*;

import com.gh.common.core.domain.entity.SysUser;
import com.gh.common.utils.DateUtils;
import com.gh.manager.clazz.domain.GhClazz;
import com.gh.manager.clazz.domain.GhUserClazz;
import com.gh.manager.clazz.mapper.GhClazzMapper;
import com.gh.manager.clazz.mapper.GhUserClazzMapper;
import com.gh.manager.train.domain.GhTrainUser;
import com.gh.manager.train.dto.ClazzUserDto;
import com.gh.manager.train.dto.TrainDetailDto;
import com.gh.manager.train.service.IGhTrainUserService;
import com.gh.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gh.manager.train.mapper.GhTrainMapper;
import com.gh.manager.train.domain.GhTrain;
import com.gh.manager.train.service.IGhTrainService;

/**
 * 训练Service业务层处理
 * 
 * @author andas
 * @date 2024-12-12
 */
@Service
public class GhTrainServiceImpl implements IGhTrainService 
{
    @Autowired
    private GhTrainMapper ghTrainMapper;
    @Autowired
    private IGhTrainUserService ghTrainUserService;

    @Autowired
    private SysUserMapper userMapper;
    // 加锁
    private Object lock = new Object();

    /**
     * 查询训练
     * 
     * @param trainId 训练主键
     * @return 训练
     */
    @Override
    public GhTrain selectGhTrainByTrainId(Long trainId)
    {
        return ghTrainMapper.selectGhTrainByTrainId(trainId);
    }

    /**
     * 查询训练列表
     * 
     * @param ghTrain 训练
     * @return 训练
     */
    @Override
    public List<GhTrain> selectGhTrainList(GhTrain ghTrain)
    {
        return ghTrainMapper.selectGhTrainList(ghTrain);
    }

    /**
     * 新增训练
     * 
     * @param ghTrain 训练
     * @return 结果
     */
    @Override
    public int insertGhTrain(GhTrain ghTrain)
    {
        ghTrain.setCreateTime(DateUtils.getNowDate());
        return ghTrainMapper.insertGhTrain(ghTrain);
    }

    /**
     * 修改训练
     * 
     * @param ghTrain 训练
     * @return 结果
     */
    @Override
    public int updateGhTrain(GhTrain ghTrain)
    {
        ghTrain.setUpdateTime(DateUtils.getNowDate());
        return ghTrainMapper.updateGhTrain(ghTrain);
    }

    /**
     * 批量删除训练
     * 
     * @param trainIds 需要删除的训练主键
     * @return 结果
     */
    @Override
    public int deleteGhTrainByTrainIds(Long[] trainIds)
    {
        return ghTrainMapper.deleteGhTrainByTrainIds(trainIds);
    }

    /**
     * 删除训练信息
     * 
     * @param trainId 训练主键
     * @return 结果
     */
    @Override
    public int deleteGhTrainByTrainId(Long trainId)
    {
        return ghTrainMapper.deleteGhTrainByTrainId(trainId);
    }

    /**
     * 添加用户到训练
     * @param trainId
     * @param userId
     * @param type
     */
    @Override
    public void join(Long trainId, Long userId, Long type) {
        GhTrainUser ghTrainUser = new GhTrainUser();
        ghTrainUser.setTrainId(trainId);
        ghTrainUser.setUserId(userId);
        ghTrainUser.setStatus(type);
        List<GhTrainUser> ghTrainUsers = ghTrainUserService.selectGhTrainUserList(ghTrainUser);
        if(ghTrainUsers==null||ghTrainUsers.size()==0){
            ghTrainUserService.insertGhTrainUser(ghTrainUser);
            //加锁更新训练人数
            synchronized (lock){
                GhTrain ghTrain = ghTrainMapper.selectGhTrainByTrainId(trainId);
                Long number = ghTrain.getNumber();
                if(number==null){
                    ghTrain.setNumber(1L);
                }else{
                    ghTrain.setNumber(number+1);
                }
                ghTrainMapper.updateGhTrain(ghTrain);
            }
        }
        //int isSuccess = ghTrainUserService.insertGhTrainUser(ghTrainUser);
    }

    /**
     * 查询参加训练的队员
     * @param trainID
     */
    @Autowired
    GhUserClazzMapper ghUserClazzMapper;
    @Autowired
    GhClazzMapper ghClazzMapper;
    @Override
    public TrainDetailDto selectUserByTrainId(GhTrain train, Long type) {
        Long trainID = train.getTrainId();
        //创建GhTrainUser对象，根据trainID查询参训人员
        GhTrainUser ghTrainUser = new GhTrainUser();
        ghTrainUser.setTrainId(trainID);
        List<GhTrainUser> ghTrainUsers = ghTrainUserService.selectGhTrainUserList(ghTrainUser);
        List<SysUser> ghUsers = new ArrayList<>();


        Map<Long,List<SysUser>> clazzMap = new Hashtable<>();

        //获取所有参训用户id封装成SysUser对象储存在List集合中
        ghTrainUsers.forEach(item->{
            if(item.getStatus().equals(type)){
                SysUser ghUser = new SysUser();
                ghUser.setUserId(item.getUserId());
                ghUsers.add(ghUser);
            }
        });
        List<SysUser> users = userMapper.selectListUser(ghUsers);

        for(int i=0;i<users.size();i++){
            GhUserClazz ghUserClazz = new GhUserClazz();
            ghUserClazz.setUserId(users.get(i).getUserId());
            List<GhUserClazz> ghUserClazzes = ghUserClazzMapper.selectGhUserClazzList(ghUserClazz);
            if(ghUserClazzes!=null && ghUserClazzes.size()!=0){
                for (int j=0;j<ghUserClazzes.size();j++){
                    if(clazzMap.containsKey(ghUserClazzes.get(j).getClazzId())){
                        clazzMap.get(ghUserClazzes.get(j).getClazzId()).add(users.get(i));
                    }else{
                        List<SysUser> userList = new ArrayList<>();
                        userList.add(users.get(i));
                        clazzMap.put(ghUserClazzes.get(j).getClazzId(),userList);
                   }
                }
            }
        }
        TrainDetailDto trainDetailDto = new TrainDetailDto();
        List<ClazzUserDto> clazzUsers = new ArrayList<>();
        clazzMap.forEach((key,value)->{
            GhClazz ghClazz = ghClazzMapper.selectGhClazzByClazzId(key);
            ClazzUserDto clazzUserDto = new ClazzUserDto();
            clazzUserDto.setClazz(ghClazz);
            clazzUserDto.setUsers(value);
            clazzUsers.add(clazzUserDto);
        });
        trainDetailDto.setClazzUsers(clazzUsers);
        SysUser newUser = new SysUser();
        newUser.setGrade(train.getGrade());
        int size = userMapper.selectUserList(newUser).size();
        trainDetailDto.setTotal(size);
        return trainDetailDto;
    }

}
